﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OrderManager.ViewModels;

namespace OrderManager
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<OrderViewModel> orders = new List<OrderViewModel>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.lstShops.ItemsSource = OrderManager.Orders.OrderManagerBase.LoadOrderManagers().Select(obj => new ViewModels.OrderManagerViewModel(obj)).ToArray();
        }

        private void btnDownload_Click(object sender, RoutedEventArgs e)
        {
            OrderManagerViewModel[] selectedOrderManagers = this.lstShops.ItemsSource.OfType<OrderManagerViewModel>().Where(obj => obj.Checked == true).ToArray();

            if (selectedOrderManagers.Length < 1)
            {
                MessageBox.Show("请选择店铺");
                return;
            }

            DownloadingWindow window = new DownloadingWindow() { OrderManagers = selectedOrderManagers.Select(obj => obj.OrderManager).ToArray() };
            bool? ret = window.ShowDialog();

            if (ret == null || ret == false)
            {
                return;
            }

            this.orders = window.Orders.Select(obj => new OrderViewModel { Order = obj, Background = Brushes.Yellow, Flag = OrderViewModel.Convert(obj.Flag) }).ToList();
            this.dgvOrders.ItemsSource = this.orders;
        }

        private void btnCheckAlreadyExist_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {

        }

        private OrderViewModel GetCurrentModel()
        {
            if (this.dgvOrders.SelectedCells.Count < 1)
            {
                return null;
            }

            return this.dgvOrders.SelectedCells[0].Item as OrderViewModel;
        }


        private void miYTArrive_Click(object sender, RoutedEventArgs e)
        {
            var order = this.GetCurrentModel();
            if (order == null)
            {
                return;
            }
        }

        private void miZhaiArrive_Click(object sender, RoutedEventArgs e)
        {
            var order = this.GetCurrentModel();
            if (order == null)
            {
                return;
            }
        }

        private void noneArrvie_Click(object sender, RoutedEventArgs e)
        {
            var order = this.GetCurrentModel();
            if (order == null)
            {
                return;
            }
        }

        private void startManualEdit_Click(object sender, RoutedEventArgs e)
        {
            var order = this.GetCurrentModel();
            if (order == null)
            {
                return;
            }
        }
    }
}
